<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="所属单位" prop="companyCode">
        <el-input v-model="formData.companyCode" placeholder="请输入所属单位" />
      </el-form-item>
      <el-form-item label="设备名称" prop="monitorName">
        <el-input v-model="formData.monitorName" placeholder="请输入设备名称" />
      </el-form-item>
      <el-form-item label="monitor_code" prop="monitorCode">
        <el-input v-model="formData.monitorCode" placeholder="请输入monitor_code" />
      </el-form-item>
      <el-form-item label="所属系统(字典fire_system_type)" prop="system">
        <el-input v-model="formData.system" placeholder="请输入所属系统(字典fire_system_type)" />
      </el-form-item>
      <el-form-item label="设备厂商（字典cfs_data_equipment_manufacturer_type）" prop="dockCode">
        <el-input v-model="formData.dockCode" placeholder="请输入设备厂商（字典cfs_data_equipment_manufacturer_type）" />
      </el-form-item>
      <el-form-item label="设备类型（关联cfs_data_manufactor_device表device_code字段）" prop="deviceCode">
        <el-input v-model="formData.deviceCode" placeholder="请输入设备类型（关联cfs_data_manufactor_device表device_code字段）" />
      </el-form-item>
      <el-form-item label="设备型号（关联cfs_data_manufactor_device表id字段）" prop="monitorType">
        <el-select v-model="formData.monitorType" placeholder="请选择设备型号（关联cfs_data_manufactor_device表id字段）">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="运营商（1中国电信 2中国移动 3中国联通）" prop="commOperator">
        <el-input v-model="formData.commOperator" placeholder="请输入运营商（1中国电信 2中国移动 3中国联通）" />
      </el-form-item>
      <el-form-item label="通讯协议（TCP、UDP、HTTP）" prop="commProtocol">
        <el-input v-model="formData.commProtocol" placeholder="请输入通讯协议（TCP、UDP、HTTP）" />
      </el-form-item>
      <el-form-item label="IMEI" prop="imei">
        <el-input v-model="formData.imei" placeholder="请输入IMEI" />
      </el-form-item>
      <el-form-item label="iccid" prop="iccid">
        <el-input v-model="formData.iccid" placeholder="请输入iccid" />
      </el-form-item>
      <el-form-item label="装机日期" prop="installDate">
        <el-date-picker
          v-model="formData.installDate"
          type="date"
          value-format="x"
          placeholder="选择装机日期"
        />
      </el-form-item>
      <el-form-item label="安装位置" prop="installAddress">
        <el-input v-model="formData.installAddress" placeholder="请输入安装位置" />
      </el-form-item>
      <el-form-item label="入网时间" prop="networkDate">
        <el-date-picker
          v-model="formData.networkDate"
          type="date"
          value-format="x"
          placeholder="选择入网时间"
        />
      </el-form-item>
      <el-form-item label="服务到期时间" prop="servicedDate">
        <el-date-picker
          v-model="formData.servicedDate"
          type="date"
          value-format="x"
          placeholder="选择服务到期时间"
        />
      </el-form-item>
      <el-form-item label="运行状态（0正常 1故障 2报警）" prop="runState">
        <el-input v-model="formData.runState" placeholder="请输入运行状态（0正常 1故障 2报警）" />
      </el-form-item>
      <el-form-item label="经度" prop="lng">
        <el-input v-model="formData.lng" placeholder="请输入经度" />
      </el-form-item>
      <el-form-item label="纬度" prop="lat">
        <el-input v-model="formData.lat" placeholder="请输入纬度" />
      </el-form-item>
      <el-form-item label="所属消防网格" prop="fireGridId">
        <el-input v-model="formData.fireGridId" placeholder="请输入所属消防网格" />
      </el-form-item>
      <el-form-item label="监测的设备类型（字典cfs_data_monitor_type）" prop="monitoringType">
        <el-select v-model="formData.monitoringType" placeholder="请选择监测的设备类型（字典cfs_data_monitor_type）">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="对接app_id" prop="appId">
        <el-input v-model="formData.appId" placeholder="请输入对接app_id" />
      </el-form-item>
      <el-form-item label="第三方数据id" prop="dataId">
        <el-input v-model="formData.dataId" placeholder="请输入第三方数据id" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input v-model="formData.createUser" placeholder="请输入创建人" />
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input v-model="formData.updateUser" placeholder="请输入修改人" />
      </el-form-item>
      <el-form-item label="安装用户类型(关联字典monitor_install_user_type)" prop="installUserType">
        <el-select v-model="formData.installUserType" placeholder="请选择安装用户类型(关联字典monitor_install_user_type)">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DataNbUsermonitorinfoApi, DataNbUsermonitorinfoVO } from '@/api/cfs/datanbusermonitorinfo'

/** 联网设备NB类型 表单 */
defineOptions({ name: 'DataNbUsermonitorinfoForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  companyCode: undefined,
  monitorName: undefined,
  monitorCode: undefined,
  system: undefined,
  dockCode: undefined,
  deviceCode: undefined,
  monitorType: undefined,
  commOperator: undefined,
  commProtocol: undefined,
  imei: undefined,
  iccid: undefined,
  installDate: undefined,
  installAddress: undefined,
  networkDate: undefined,
  servicedDate: undefined,
  runState: undefined,
  lng: undefined,
  lat: undefined,
  fireGridId: undefined,
  monitoringType: undefined,
  appId: undefined,
  dataId: undefined,
  status: undefined,
  createUser: undefined,
  updateUser: undefined,
  installUserType: undefined,
})
const formRules = reactive({
  runState: [{ required: true, message: '运行状态（0正常 1故障 2报警）不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DataNbUsermonitorinfoApi.getDataNbUsermonitorinfo(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DataNbUsermonitorinfoVO
    if (formType.value === 'create') {
      await DataNbUsermonitorinfoApi.createDataNbUsermonitorinfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await DataNbUsermonitorinfoApi.updateDataNbUsermonitorinfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    companyCode: undefined,
    monitorName: undefined,
    monitorCode: undefined,
    system: undefined,
    dockCode: undefined,
    deviceCode: undefined,
    monitorType: undefined,
    commOperator: undefined,
    commProtocol: undefined,
    imei: undefined,
    iccid: undefined,
    installDate: undefined,
    installAddress: undefined,
    networkDate: undefined,
    servicedDate: undefined,
    runState: undefined,
    lng: undefined,
    lat: undefined,
    fireGridId: undefined,
    monitoringType: undefined,
    appId: undefined,
    dataId: undefined,
    status: undefined,
    createUser: undefined,
    updateUser: undefined,
    installUserType: undefined,
  }
  formRef.value?.resetFields()
}
</script>